package com.vecspace.web.freemarker;

import java.util.Locale;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.vecspace.exception.HException;

/**从资源文件中加载模版文件  
 * @author: wanghua
 */
public class FreeMarkerViewResolverEx extends FreeMarkerViewResolver{
	/**调试状态，处于调试状态时，每次清空FreeMarker的缓存*/
	private boolean useCache = false;
	
	@Override
	protected AbstractUrlBasedView buildView(String viewName) throws Exception {		
		if(viewName != null && viewName.startsWith("ftl:")){			
			AbstractUrlBasedView view = super.buildView(viewName);		
			return view;
		}else return null;
	}

	@Override
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		if(viewName == null || !viewName.startsWith("ftl:")){
			return null;
		}
		
		if(!useCache){
			this.clearCache();
		}
		View view = super.resolveViewName(viewName, null);
		if(view == null){
			throw new HException("模版文件不存在："+viewName);
		}
		return view;
	}

	public boolean isUseCache() {
		return useCache;
	}

	public void setUseCache(boolean useCache) {
		this.useCache = useCache;
	}
	
	
}
